package com.business.etl.flow.status;

import com.component.api.constants.FlowStatus;
import com.component.api.constants.NodeStatus;
import com.component.api.constants.StatusContents;
import com.framework.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 描述：
 * 状态服务管理类
 * @author xianggj
 * @Date 2021/11/3 15:26
 **/
@Service
public class StatusManager {


    @Autowired
    private RedisService redisService;


    /**
     * 设置流程状态
     * @param flowId
     * @param flowStatus
     */
    public void setFlowStatus(String flowId, FlowStatus flowStatus){
        redisService.set(StatusContents.FLOW_STATUS_KEY + flowId, flowStatus.toString());
        redisService.hset(StatusContents.STATUS_KEY + flowId, StatusContents.FLOW_STATUS, flowStatus.toString());
        switch (flowStatus) {
            case START:
                redisService.hset(StatusContents.STATUS_KEY + flowId, StatusContents.START_TIME,
                        String.valueOf(System.currentTimeMillis()));
                break;
            case STOP:
            case FAIL:
            case FINISH:
                redisService.hset(StatusContents.STATUS_KEY + flowId, StatusContents.END_TIME,
                        String.valueOf(System.currentTimeMillis()));
                break;
            default:

        }

    }

    /**
     * 获取流程状态
     * @param flowId
     */
    public String getFlowStatus(String flowId){
        String hget = redisService.get(StatusContents.FLOW_STATUS_KEY + flowId);
        return hget;

    }
    /**
     * 设置流程状态
     * @param flowId
     * @param nodeId
     * @param nodeStatus
     */
    public void setNodeStatus(String flowId, String nodeId, NodeStatus nodeStatus){
        redisService.hset(StatusContents.STATUS_KEY + flowId, nodeId, nodeStatus.toString());
        switch (nodeStatus) {
            case INIT:
                redisService.hset(StatusContents.STATUS_KEY + flowId,
                        nodeId + StatusContents.DECOLLATOR + StatusContents.INIT_TIME,
                        String.valueOf(System.currentTimeMillis()));
                break;
            case RUNNING:
                redisService.hset(StatusContents.STATUS_KEY + flowId,
                        nodeId + StatusContents.DECOLLATOR + StatusContents.START_TIME,
                        String.valueOf(System.currentTimeMillis()));
                break;
            case FINISH:
            case FAIL:
                redisService.hset(StatusContents.STATUS_KEY + flowId,
                        nodeId + StatusContents.DECOLLATOR + StatusContents.END_TIME,
                        String.valueOf(System.currentTimeMillis()));
                break;
            default:

        }

    }

}
